import RenderCell from 'packages/BaseTableCell/renderCell.js';
import formatColumn from 'packages/BaseTableCell/formatColumn';
import { formatDate } from 'pub-bbx-utils';
import i18n from '@src/locales'

// 高级搜索
export const getAdvancedFields = (vm, {AllPutChannel = [], AllServiceNumber = []}) => {
  let that = vm;
  return [
    // {
    //   fieldName: 'source', 
    //   displayName: i18n.t('common.fields.imSource.displayName'),
    //   formType: 'select',
    //   setting: {
    //     isMulti: false,
    //     dataSource: [
    //       {
    //         text: i18n.t('common.fields.imSource.dataSource.text1'),
    //         value: 'im'
    //       },
    //       {
    //         text: i18n.t('common.fields.imSource.dataSource.text2'),
    //         value: 'weChat'
    //       },
    //       {
    //         text: i18n.t('common.fields.imSource.dataSource.text3'),
    //         value: 'emailService'
    //       }
    //     ]
    //   }
    // },
    {
      fieldName: 'state',
      displayName: `${i18n.t('common.base.haveRead')}/${i18n.t('common.base.notRead')}`,
      formType: 'select',
      setting: {
        isMulti: false,
        dataSource: [
          {
            text: i18n.t('common.base.haveRead'),
            value: 'read'
          },
          {
            text: i18n.t('common.base.notRead'),
            value: 'unread'
          }
        ]
      }
    },
    {
      fieldName: 'conversationStatus',
      displayName: i18n.t('common.fields.conversationStatus.displayName'),
      formType: 'select',
      defaultValue:2,
      useDefaultValue: true,
      setting: {
        isMulti: false,
        dataSource: [
          {
            text:  i18n.t('common.fields.conversationStatus.datasource.text3'),
            value: 2
          },
          {
            text:  i18n.t('common.fields.conversationStatus.datasource.text4'),
            value: 3
          },
          {
            text:  i18n.t('common.base.all'),
            value: 0
          },
        ]
      }
    },
    {
      fieldName: 'putChannelIds',
      displayName: i18n.t('common.fields.imPutChannelId.displayName'),
      formType: 'select',
      setting: {
        isMulti: false,
        dataSource:AllPutChannel
      }
    }
  ].map(item => {
    return {
      ...item,
      setting: item.setting || {},
      defaultValue: item?.defaultValue ?? null,
      show: true,
      isSearch: 1,
      isNull: 1,
      isSystem: 1,
      orderId: 1
    }
  });
}


// 列表 && 选择列
export const getColumnFields = (vm) => {
  const that = vm;

  return [
    {
      fieldName: 'conversationNumber',
      displayName: i18n.t('common.fields.imConversationNumber.displayName'),
      sortName: 'conversationNumber',
      dataType: 'string',
      renderCell: (h, col, row) => {
        return RenderCell.renderIm.call(that, h, row, that.allowImView);
      }
    },
    {
      fieldName: 'customerName',
      displayName: i18n.t('common.fields.customerName.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.customerName}</div>
      }
    },
    {
      fieldName: 'linkmanName',
      displayName: i18n.t('common.fields.contact.displayName'),
      renderCell: (h, col, row) => {
        if (row.source == 'weChat' && row.linkmanName) {
          return <div>{row.linkmanName}<span class="wx-im iconfont icon-weixin5"></span></div>
        }
        return <div>{row.linkmanName}</div>
      }
    },
    {
      fieldName: 'phoneNumber',
      displayName: i18n.t('common.fields.phone.displayName'),
      renderCell: (h, col, row) => {
        return <div>{row.phoneNumber}</div>
      }
    },
    {
      fieldName: 'address',
      displayName: i18n.t('common.fields.address.displayName'),
      width: 210,
      renderCell: (h, col, row) => {
        return <div>{row.address}</div>
      }
    },
    {
      fieldName: 'customerServiceNumber',
      displayName: i18n.t('common.fields.customerServiceNumber.displayName'),
      sortName: 'customerServiceNumber',
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.customerServiceNumber}</div>
      }
    },
    {
      fieldName: 'customerServiceName',
      displayName: i18n.t('common.fields.customerServiceName.displayName'),
      sortName: 'customerServiceName',
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.customerServiceName}</div>
      }
    },
    {
      fieldName: 'relationUserId',
      displayName: i18n.t('common.fields.relationUserId.displayName'),
      sortName: 'relationUserId',
      dataType: 'string',
      width: 210,
      renderCell: (h, col, row) => {
        return <div>{row.relationUserName}</div>
      }
    },
    {
      fieldName: 'startTime',
      displayName: i18n.t('common.fields.imStartTime.displayName'),
      sortable: 'custom',
      sortName: 'startTime',
      dataType: 'date',
      renderCell: (h, col, row) => {
        return <div>{formatDate(row.startTime, 'YYYY-MM-DD HH:mm:ss')}</div>;
      }
    },
    {
      fieldName: 'endTime',
      displayName: i18n.t('common.fields.imEndTime.displayName'),
      sortable: 'custom',
      sortName: 'endTime',
      dataType: 'date',
      renderCell: (h, col, row) => {
        return <div>{formatDate(row.endTime, 'YYYY-MM-DD HH:mm:ss')}</div>;
      }
    },
    {
      fieldName: 'source',
      displayName: i18n.t('common.fields.imSource.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.source == 'weChat' ? i18n.t('common.fields.imSource.dataSource.text2') : i18n.t('common.fields.imSource.dataSource.text1')}</div>
      }
    },
    {
      fieldName: 'putChannelName',
      displayName: i18n.t('common.fields.imPutChannelId.displayName'),
      dataType: 'string',
      renderCell: (h, col, row) => {
        return <div>{row.putChannelName ? row.putChannelName.join(',') : ''}</div>
      }
    },
  ].map(item => {
    // 初始化属性
    let field = {
      ...item,
      formType: 'text',
      setting: item.setting || {},
      isNull: 1,
      isSearch: 1,
      orderId: 1,
      show: true,
      tableName: 'event',
      minWidth: 150,
      visable: typeof item.visable == 'undefined' ? true : item.visable
    };
    return formatColumn(field);
  })
};

export const getExportSystemInfo = () => {
  return [
    {
      exportAlias: 'conversationNumber',
      displayName: i18n.t('common.fields.imConversationNumber.displayName'),
    },
    {
      exportAlias: 'customerName', // customerId
      displayName: i18n.t('common.fields.customerName.displayName'),
    },
    {
      exportAlias: 'linkmanName',
      displayName: i18n.t('common.fields.contact.displayName'),
    },
    {
      exportAlias: 'phoneNumber',
      displayName: i18n.t('common.fields.phone.displayName'),
    },
    {
      exportAlias: 'address',
      displayName: i18n.t('common.fields.address.displayName'),
    },
    {
      exportAlias: 'customerServiceNumber',
      displayName: i18n.t('common.fields.customerServiceNumber.displayName'),
    },
    {
      exportAlias: 'customerServiceName',
      displayName: i18n.t('common.fields.customerServiceName.displayName'),
    },
    {
      exportAlias: 'relationUserId',
      displayName: i18n.t('common.fields.relationUserId.displayName'),
    },
    {
      exportAlias: 'imStartTime',
      displayName: i18n.t('common.fields.imStartTime.displayName'),
    },
    {
      exportAlias: 'imEndTime',
      displayName: i18n.t('common.fields.imEndTime.displayName'),
    },
    {
      exportAlias: 'source',
      displayName: i18n.t('common.fields.imSource.displayName'),
    },
    {
      exportAlias: 'putChannel',
      displayName: i18n.t('common.fields.imPutChannelId.displayName'),
    },
  ]
}